package com.sharemarking.application.common.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.entity.Fab;
import com.sharemarking.application.common.entity.Share;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.FabRepo;
import com.sharemarking.application.common.repository.ShareRepo;

@Controller
@RequestMapping("fab1")
public class FabController1 {

	@Autowired
	FabRepo fr;
	
	@Autowired
	EmployeeRepo er;
	
	@Autowired
	ShareRepo sr;
	
	@RequestMapping("ffff")
	@ResponseBody
	public List<Fab> getAllFab(){
		List<Fab> list=null;
		list=(List<Fab>) fr.findAll();
		return list;
	}

	// 通过分享id查找点赞数据列表
	@RequestMapping("list11")
	@ResponseBody
	public List<Fab> getAllFab(HttpServletResponse res,Integer id) {
		/** 设置响应头允许ajax跨域访问 **/
		res.setHeader("Access-Control-Allow-Origin", "*");
		System.out.println("sid:"+id);
		List<Fab> list = null;
		List<Fab> list1=new ArrayList<Fab>();
		Sort sort = new Sort(Direction.DESC, "id");
		list = (List<Fab>) fr.findAll(sort);
		
		for(int i=0;i<list.size();i++){
			if(list.get(i).getShare().getId().equals(id)){
				list1.add(list.get(i));
			}
		}
		return list1;
	}
	
	//检验图文信息是否被点赞了
	@RequestMapping("checkFab")
	@ResponseBody
	public String checkFab(Integer sid,String loginName,Integer eid){
		System.out.println("eid:"+eid);
		Employee e1=er.findOne(eid);
		Share s=sr.findOne(sid);
		String result="";
		
		if(e1!=null&&s!=null){
			result="true";
			return result;
		}else{
			result="false";
			return result;
		} 
	}
	
	//添加点赞数据v 加1
	@RequestMapping("addPraise")
	@ResponseBody
	public void addFab1(String loginName,Integer sid,Integer fid){
		System.out.println("+++++++++++++++++++++++++sid:"+sid);
		Fab fab=new Fab();
		
		Employee e=er.findByLoginName(loginName);
		Share s=sr.findOne(sid);
		
		if(s!=null&&e!=null){
			
			fab.setEmployee(e);
			fab.setShare(s);
			fab.setState("已点赞");
			fr.save(fab);
		} 		
	}
	
		//点赞减一
		@RequestMapping("delPraise")
		@ResponseBody
		public void removeOne(String loginName,Integer fid){
			System.out.println("+++++++++++++++++++++++++fid:"+fid);
			System.out.println("+++++++++++++++++++++++++loginName:"+loginName);
				fr.delete(fid);
		}
		
		
		//判断是否点赞
		@RequestMapping("checkPraise")
		@ResponseBody
		public String checkFab(HttpServletResponse res,Integer id,String loginName) {
			/** 设置响应头允许ajax跨域访问 **/
			res.setHeader("Access-Control-Allow-Origin", "*");
			System.out.println("sid:"+id);
			List<Fab> list = null;
			List<Fab> list1=new ArrayList<Fab>();
			Sort sort = new Sort(Direction.DESC, "id");
			list = (List<Fab>) fr.findAll(sort);
			Employee e=er.findByLoginName(loginName);
			String result="未点赞";
			
			for(int i=0;i<list.size();i++){
				if(list.get(i).getShare().getId().equals(id)){
					list1.add(list.get(i));
				}
			}
			for(int a=0;a<list1.size();a++){
				if(list1.get(a).getEmployee().getId().equals(e.getId())){
					result="已点赞";
				}
			}
			
			return result;
		}
}
